package com.jiutong.meipenggang.ui.activitys;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.adapters.PayOutListAdapter;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.PayOut2Bean;
import com.jiutong.meipenggang.entity.SurePayOut2Bean;
import com.jiutong.meipenggang.http.model.interfaces.MeiCaiBaoBiz;
import com.jiutong.meipenggang.http.present.MeiCaiBaoPresent;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.views.dialog.BaseDialog;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jiutong.meipenggang.R.id.cb_payout;

public class PayOut2Activity extends BaseActivity implements MeiCaiBaoBiz {

    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.tv_common_title_options)
    TextView tvCommonTitleOptions;
    @Bind(R.id.rv_payout_list)
    RecyclerView rvPayoutList;

    @Bind(R.id.btn_sure_payout)
    Button btnSurePayout;
    @Bind(R.id.activity_pay_out)
    RelativeLayout activityPayOut;
    private MeiCaiBaoPresent meiCaiBaoPresent;
    private PayOutListAdapter payOutListAdapter;

    private BaseDialog dialog;
    private View inflate;
    private Button sure_pay;
    private CheckBox checkBox;
    private List<PayOut2Bean.ListBean> listbean;
    private int inverstId = 0;
    private PopupWindow popupWindow;
    private int index = -1;
    private Double price = 0.00;
    private TextView sure_pay_price;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_pay_out2;
    }


    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tvCommonTitle.setText("转出");

    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        meiCaiBaoPresent = new MeiCaiBaoPresent(this, this);
        meiCaiBaoPresent.payout2();
    }

    @Override
    public void onSuccess(Object object) {
        PayOut2Bean respone = (PayOut2Bean) object;
        SpConfig.getInstance().edit().putString(Constants.TOKEN, respone.getToken()).commit();
        setAdapter(respone);
    }

    private void setAdapter(PayOut2Bean respone) {
        listbean = respone.getList();
        payOutListAdapter = new PayOutListAdapter(this, R.layout.item_payout, listbean);
        rvPayoutList.setLayoutManager(new LinearLayoutManager(this));
        payOutListAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, final int i) {
                checkBox = (CheckBox) view.findViewById(cb_payout);
                inverstId = listbean.get(i).getInvestId();
                setCheckBox(checkBox, i);
                index = i;
                price = listbean.get(i).getAmount();
                checkBox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setCheckBox(checkBox, i);
                    }
                });
            }
        });

        rvPayoutList.setAdapter(payOutListAdapter);
    }

    //checkbox单选逻辑

    private void setCheckBox(final CheckBox checkBox, final int position) {
        if (checkBox.isChecked()) {
            checkBox.setChecked(false);
            for (int q = 0; q < listbean.size(); q++) {
                if (q == position) {
                    listbean.get(position).setCb(false);
                } else {
                }
            }
            payOutListAdapter.notifyDataSetChanged();
        } else {

            for (int q = 0; q < listbean.size(); q++) {
                if (q == position) {
                    listbean.get(position).setCb(true);
                } else {
                    listbean.get(q).setCb(false);
                }
            }
            payOutListAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 0;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }


    @OnClick({R.id.rl_common_title_back, R.id.btn_sure_payout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                finish();
                break;
            case R.id.btn_sure_payout:
                if (index != -1) {
                    setPop();
                } else {
                    toastShortShow("请选择需要转出的标的!");
                }
                break;
        }
    }


    private void setPop() {
        inflate = LayoutInflater.from(this).inflate(R.layout.pop_payout, null);
        sure_pay = (Button) inflate.findViewById(R.id.btn_sure_payout2);
        sure_pay_price = (TextView) inflate.findViewById(R.id.pop_sure_price);
        popupWindow = new PopupWindow(this);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(inflate);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setAnimationStyle(R.style.dialog_animation_fade);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(activityPayOut, Gravity.CENTER, 0, 0);
        sure_pay_price.setText("转出金额"+price + "元");
        sure_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                meiCaiBaoPresent.surePayOut2(inverstId);
            }
        });
    }


    @Override
    public void onSuccess2(Object object) {
        SurePayOut2Bean respone = (SurePayOut2Bean) object;
        if (respone.getError() == -1) {
            toastShortShow("转出成功!");
            payOutListAdapter.remove(index);
            payOutListAdapter.notifyDataSetChanged();
            index = -1;
            popupWindow.dismiss();
            SpConfig.getInstance().edit().putString(Constants.TOKEN, respone.getToken()).commit();
        }
    }

    @Override
    public void onError2(Throwable e) {

    }
}
